package com.android.sharecar.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.sharecar.model.PickRideRequestMsg;
import com.android.sharecar.model.User;
import com.android.sharecar.util.BitmapUtil;
import com.google.android.maps.GeoPoint;

/**
 * This activity display the real time pick ride request list...
 * 
 * @author xinyan
 * @date 2011-10-13
 */
public class PickRideRequestListActivity extends ListActivity implements
		OnTouchListener, OnGestureListener {
	public static final String TAG = "PickRideRequestListActivity";

	// Views
	private ImageButton mHome;

	// Members
	private static List<PickRideRequestMsg> mRequestMsgs;

	// Gesture detector that used to switch activity by gesture.
	private GestureDetector mGestureDetector;

	private static int mVerticalMinDistance = 70;

	private static int mMinVelocity = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final boolean customTitle = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.list_pickride_request);

		if (customTitle) {
			getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
					R.layout.titilebar_pickride_list);
		}
		mHome = (ImageButton) findViewById(R.id.realTimeRequestList_ib_home);
		mHome.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(),
						MainTabWidgetActivity.class);
				intent.putExtra(MainTabWidgetActivity.CURRENT_TAB,
						MainTabWidgetActivity.CURRENT_TAB_NORMAL);
				startActivity(intent);
			}
		});

		searchUsersFromServerByGeoPoint(null);
		setListAdapter(new RequestListAdapter(this,
				R.layout.list_item_pickride_request, mRequestMsgs));
		setSelection(6);

		mGestureDetector = new GestureDetector(this);
		LinearLayout rootLayout = (LinearLayout) findViewById(R.id.pickRideRequestList_rootLayout);
		rootLayout.setOnTouchListener(this);
		rootLayout.setLongClickable(true);
	}

	private void searchUsersFromServerByGeoPoint(GeoPoint point) {
		mRequestMsgs = new ArrayList<PickRideRequestMsg>();
		PickRideRequestMsg msg;
		User user;
		Bitmap headPic;
		BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources()
				.getDrawable(R.drawable.myheadpic);
		headPic = bitmapDrawable.getBitmap();

		for (int i = 0; i < 8; i++) {
			user = new User();
			msg = new PickRideRequestMsg();

			user.setNickName("用户" + i);
			user.setGender("男");
			user.setHeadPicture(BitmapUtil.getBytes(headPic));

			msg.setUser(user);
			msg.setStartPlace("二拨子");
			msg.setEndPlace("天安门");
			msg.setPostTime(new Date());

			mRequestMsgs.add(msg);
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent();
		intent.setClass(this, PickRideRequestDetailActivity.class);
		startActivity(intent);
	}

	private final class RequestListAdapter extends BaseAdapter {
		private final Context mContext;

		private final Integer mResource;

		private final List<PickRideRequestMsg> mData;

		public RequestListAdapter(Context context, Integer resource,
				List<PickRideRequestMsg> value) {
			this.mContext = context;
			this.mResource = resource;
			this.mData = value;
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Log.i(TAG, "getView-----" + position + "---convertView--"
					+ convertView);
			TextView startPlace, destination, postTime;
			View view = View.inflate(mContext, mResource, null);
			ImageView headPic = (ImageView) view
					.findViewById(R.id.realTimeRequestList_iv_headPic);
			/*
			 * nickName = (TextView) view
			 * .findViewById(R.id.realTimeRequestList_tv_nickName); gender =
			 * (TextView) view
			 * .findViewById(R.id.realTimeRequestList_tv_gender);
			 */
			startPlace = (TextView) view
					.findViewById(R.id.realTimeRequestList_tv_startPlace);
			destination = (TextView) view
					.findViewById(R.id.realTimeRequestList_tv_endPlace);
			postTime = (TextView) view
					.findViewById(R.id.realTimeRequestList_tv_postTime);

			PickRideRequestMsg msg = mData.get(position);
			if (null != msg) {
				User user = msg.getUser();
				if (null != user) {
					headPic.setImageBitmap(BitmapUtil.getBitmap(user
							.getHeadPicture()));
					/*
					 * nickName.setText(user.getNickName());
					 * gender.setText(user.getGender());
					 */
				}
				startPlace.setText(msg.getStartPlace());
				destination.setText(msg.getEndPlace());
				postTime.setText(msg.getPostTime().toLocaleString());
			}
			return view;
		}

	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {

	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (e1.getX() - e2.getX() > mVerticalMinDistance
				&& Math.abs(velocityX) > mMinVelocity) {

			// 向左手势
			Intent intent = new Intent(this,
					ReserveRequestListTabWidgetActivity.class);
			startActivity(intent);
		} else if (e2.getX() - e1.getX() > mVerticalMinDistance
				&& Math.abs(velocityX) > mMinVelocity) {

			// 向右手势
			Intent intent = new Intent(this, OnlineUserListActivity.class);
			startActivity(intent);
		}
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		mGestureDetector.onTouchEvent(ev);
		return super.dispatchTouchEvent(ev);
	}
}
